package com.ccx.coachsite.ui.manage;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CommonBus;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.ui.own.OwerAdapterBase;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.util.Log;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;
import com.ccx.coachsite.webimage.ImageLoader;

public class ManageCustomInviteListAdapter extends OwerAdapterBase<CustomInfo>{
	private boolean isInvited;
	private OwerBaseView superView;
	public ManageCustomInviteListAdapter(Context context, List<CustomInfo> list,OwerBaseView superView , boolean isInvited) {
		super(context, list);
		this.superView = superView;
		this.isInvited = isInvited;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(position == 0)
			Log.e("load:"+position);
		
		convertView = mInflater.inflate(R.layout.manage_custom_invited_list_item, parent, false);
		holder = new ViewHolder();
		holder.nName = (TextView) convertView.findViewById(R.id.manage_custom_invite_title);
		holder.nBaseLine = (TextView) convertView.findViewById(R.id.manage_custom_invite_baseline);
		holder.nMode = (TextView) convertView.findViewById(R.id.manage_custom_invite_mode);
		holder.mIcon = (ImageView) convertView.findViewById(R.id.manage_custom_invite_image);
		holder.nButton = (Button) convertView.findViewById(R.id.manage_custom_invite_buttom);
		/*
		if(convertView == null) {
			convertView = mInflater.inflate(R.layout.manage_custom_invited_list_item, parent, false);
			holder = new ViewHolder();
			holder.nName = (TextView) convertView.findViewById(R.id.manage_custom_invite_title);
			holder.nBaseLine = (TextView) convertView.findViewById(R.id.manage_custom_invite_baseline);
			holder.nMode = (TextView) convertView.findViewById(R.id.manage_custom_invite_mode);
			holder.mIcon = (ImageView) convertView.findViewById(R.id.manage_custom_invite_image);
			holder.nButton = (Button) convertView.findViewById(R.id.manage_custom_invite_buttom);
			convertView.setTag(holder);
		}else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		*/
		int id = mList.get(position).getId();
		if(id == -1)
		{
			holder.mIcon.setVisibility(View.INVISIBLE);
			holder.nName.setText("邀请全部客户");
			holder.nMode.setText("一键快捷邀请客户加入");
			holder.nBaseLine.setVisibility(View.GONE);
			holder.nButton.setText("全部邀请");
			holder.nButton.setTextColor(mContext.getResources().getColor(R.color.blue_title_color));
			holder.nButton.setOnClickListener(new OnAllIntiveListener());
		}
		else
		{
			holder.nName.setText(mList.get(position).getName());
			holder.nBaseLine.setText("基准值:"+mList.get(position).getBaseLine());
			holder.nMode.setText("机型:"+mList.get(position).getMode());
			if(isInvited)
			{
				holder.nButton.setText(mList.get(position).getMessageCount()+"条未读");
				holder.nButton.setTextColor(mContext.getResources().getColor(R.color.red));
				holder.nButton.setOnClickListener(new OnMessageListener());
				holder.nButton.setTag(mList.get(position));
			}
			else
			{
				holder.nButton.setText("邀请");
				holder.nButton.setTextColor(mContext.getResources().getColor(R.color.blue_title_color));
				holder.nButton.setOnClickListener(new OnIntiveListener());
				holder.nButton.setTag(mList.get(position).getTianYi());
			}
			
			String picturePath = mList.get(position).getIcon();
			if (!StringUtil.isEmpty(picturePath)) 
			{
				holder.mIcon.setImageResource(R.drawable.ower_image_none);
				ImageLoader.loadBitmap(picturePath, holder.mIcon);
			}	
		}
		
		
		return convertView;
	}
	
	public void doInvate(String mobiles)
	{
		CommonBus bus = new CommonBus(mContext);
		String userId = SharedPreferencesUtil.getValue(mContext,
				SharedKey.Shared_UserId_Key, "");
		bus.setInviteParam(userId, mobiles);
		bus.doTask();
	}
	
	class OnIntiveListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Object obj = v.getTag();
			if(obj != null)
			{
				String mobile = v.getTag().toString();
				doInvate(mobile);
			}
		}
	}
	
	class OnAllIntiveListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			String message = "";
			for(int i = mList.size() - 1; i>= 0; i--)
			{
				message += mList.get(i).getId()+",";
			}
			if(message.length() > 0)
				message = message.substring(0, message.length() - 1);
			
			if(!StringUtil.isEmpty(message))
			{
				doInvate(message);
			}
		}
	}
	
	class OnMessageListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Object obj = v.getTag();
			if(obj != null)
			{
				CustomInfo cinfo = (CustomInfo)v.getTag();
				cinfo.setMessageBtn((Button)v);
				superView.SwitchView(ManageKey.ll_manage_custom_message, cinfo);
			}
		}
	}
	
	class ViewHolder {
		ImageView mIcon;
		TextView nName;
		TextView nBaseLine;
		TextView nMode;
		Button nButton;
	}

}
